package net.cardgame.orcalecard;

import jp.jma.oraclecard.R;
import net.cardgame.oraclecard.common.MySwitch;
import net.cardgame.orcalecard.bean.SettingApp;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;

public class SettingSortActivity extends Activity implements OnClickListener,
		OnCheckedChangeListener {
	int sortby = 0;
	MySwitch switchRelease, switchPayment, switchUsed;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.setting_sort_layout);
		findViewById(R.id.btn_back_setting_sort).setOnClickListener(this);
		Bundle bundle = this.getIntent().getExtras();
		sortby = bundle.getInt("sort_by");
		switchRelease = (MySwitch) findViewById(R.id.switch_by_release);
		switchPayment = (MySwitch) findViewById(R.id.switch_by_payment);
		switchUsed = (MySwitch) findViewById(R.id.switch_by_used);
		switchUsed.setOnCheckedChangeListener(this);
		switchRelease.setOnCheckedChangeListener(this);
		switchPayment.setOnCheckedChangeListener(this);
		switchUsed.setChecked(false);
		switchRelease.setChecked(false);
		switchPayment.setChecked(false);
		switch (sortby) {
		case SettingApp.SORT_BY_RELEASE:
			switchRelease.setChecked(true);
			break;
		case SettingApp.SORT_BY_USED:
			switchUsed.setChecked(true);
			break;
		case SettingApp.SORT_BY_BUY:
			switchPayment.setChecked(true);
			break;
		default:
			break;
		}
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		setResult(sortby);
		this.finish();
	}

	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		// TODO Auto-generated method stub
		if (!isChecked && !switchUsed.isChecked() && !switchRelease.isChecked()
				&& !switchPayment.isChecked()) {
			sortby = SettingApp.SORT_BY_RELEASE;
		}

		switch (buttonView.getId()) {
		case R.id.switch_by_payment:
			if (isChecked) {
				sortby = SettingApp.SORT_BY_BUY;
				switchUsed.setChecked(false);
				switchRelease.setChecked(false);
			}
			break;
		case R.id.switch_by_release:
			if (isChecked) {
				sortby = SettingApp.SORT_BY_RELEASE;
				switchUsed.setChecked(false);
				switchPayment.setChecked(false);
			}
			break;
		case R.id.switch_by_used:
			if (isChecked) {
				sortby = SettingApp.SORT_BY_USED;
				switchRelease.setChecked(false);
				switchPayment.setChecked(false);
			}
			break;

		default:
			break;
		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			setResult(sortby);
			this.finish();
		}
		return super.onKeyDown(keyCode, event);
	}
}
